package com.xblibo.xruth.common;

import java.util.Calendar;
import java.util.Date;

import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;

/**
 * <p>Title: DateUtil.java </p>
 * <p>Description:  </p>
 * <p>Copyright: 易宝 2011*版权所有 </p>
 * <p>Company: yeepay </p>
 * @author xblibo
 * @version 1.0 ,  2011  上午09:55:08
 */
public class DateUtil {
	
	public static Date getWeekStartDate(Date date){
		 Calendar  calendar  =   Calendar.getInstance();
		 calendar.setTime(date);
		 int day  =  calendar.get(Calendar.DAY_OF_WEEK);
		 if(day == 1){
			 day = 8;
		 }
		 System.out.println(day);
		 calendar.add(Calendar.DAY_OF_WEEK, 2-day);
		 Date startDate = calendar.getTime();
		 System.out.println(DateFormatUtils.format(calendar, "yyyy-MM-dd"));
		 calendar.add(Calendar.DAY_OF_WEEK, 6);
		 System.out.println(DateFormatUtils.format(calendar, "yyyy-MM-dd"));
		 
		 return startDate;
	}
	
	public static String getWeekStartDate(int month,int week){
		Calendar  calendar  =   Calendar.getInstance();
		calendar.add(Calendar.MONDAY,month-7);
		calendar.add(Calendar.WEEK_OF_MONTH,week);
		
		 System.out.println(DateFormatUtils.format(calendar, "yyyy-MM-dd"));
		 return null;
	}
	public static void getWeekStartDate(int month){
		Calendar  calendar  =   Calendar.getInstance();
		calendar.set(Calendar.MONTH, month-1);
		calendar.set(Calendar.DAY_OF_MONTH,1);
		
		int week = calendar.get(Calendar.DAY_OF_WEEK);
		
		calendar.add(Calendar.DAY_OF_WEEK, week+1-Calendar.SUNDAY);
		
		System.out.println(DateFormatUtils.format(calendar, "yyyy-MM-dd"));
	}
	
	public static void getWeekEndDate(int month){
		Calendar  calendar  =   Calendar.getInstance();
		calendar.set(Calendar.MONTH, month-1);
		calendar.set(Calendar.DAY_OF_MONTH,1);
		calendar.roll(Calendar.DAY_OF_MONTH, -1);
		
		int week = calendar.get(Calendar.DAY_OF_WEEK);
		System.out.println(week);
		calendar.add(Calendar.DAY_OF_WEEK, Calendar.SUNDAY-week);
		
		System.out.println(DateFormatUtils.format(calendar, "yyyy-MM-dd"));
	}

	/**
	 * @param args
	 * @return void
	 * author  xblibo  2011 上午09:55:08
	 */
	public static void main(String[] args) {
		getWeekStartDate(DateUtils.addDays(new Date(), 1));
		
		getWeekEndDate(7);
	}

}
